package view;

import java.awt.EventQueue;

import javax.swing.JFrame;

import action.Action;
import model.Program;

import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import java.util.Vector;
import java.awt.event.ActionEvent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JMenuBar;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JList;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class MainGUI {

	private JFrame frmClumsycrawler;
	
	private JPanel defaultPanel;
	
	private JPanel panelForAddRecord;
	private JTextField textField_5;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_7;
	private JTextField textField_4;
	private JTextField textField_6;
	private JTextField textField_3;
	private JTextField textField_8;
	private JTextField textField_9;
	private JTextField textField_10;
	private JTextField textField_12;
	private JTextField textField_11;
	
	private JPanel panelForQuery;
	private JTextField textField;
	private JList<String> list;
	private final JScrollPane scrollPane = new JScrollPane();

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainGUI window = new MainGUI();
					window.frmClumsycrawler.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmClumsycrawler = new JFrame();
		frmClumsycrawler.setTitle("ClumsyCrawler");
		frmClumsycrawler.setBounds(100, 100, 939, 668);
		frmClumsycrawler.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frmClumsycrawler.getContentPane().setLayout(null);
		
		showMenuBar();

		showDefaultPanel();
		
	}
	
	
	/**
	 * 显示查询面板
	 */
	private void showPanelForQuery() {
		frmClumsycrawler.getContentPane().removeAll();
		
		panelForQuery = new JPanel();
		panelForQuery.setBounds(0, 0, 923, 608);
		frmClumsycrawler.getContentPane().add(panelForQuery);
		panelForQuery.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(43, 22, 488, 38);
		panelForQuery.add(textField);
		textField.setColumns(10);
		
		JButton btnQuery = new JButton("Query");
		btnQuery.setBounds(649, 22, 237, 38);
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String queryStatment = textField.getText().trim();//去掉两头的空字符
				List<Program> programs=  Action.query(queryStatment);
				
				//构造一个字符串向量作为list的内容
				Vector<String> strings = new Vector<String>();
				for(Program program:programs)
				{
					strings.addElement(program.toString());
				}
				
				list.setListData(strings);			
			}
		});
		btnQuery.setFont(new Font("SimSun", Font.PLAIN, 26));
		btnQuery.setForeground(Color.BLACK);
		panelForQuery.add(btnQuery);
		scrollPane.setBounds(43, 70, 844, 528);
		panelForQuery.add(scrollPane);
		
		list = new JList<String>();
		list.setForeground(new Color(0, 128, 0));
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String selectedValue = list.getSelectedValue();
				Program p = Action.parseSelectedlString2Program(selectedValue);
				SpecificProgramInfo.showMesDialog(p);
			}
		});
		list.setBackground(new Color(211, 211, 211));
		list.setFont(new Font("SimSun", Font.PLAIN, 16));
		scrollPane.setViewportView(list);
		
		frmClumsycrawler.getContentPane().repaint();// 重绘
	}
	
	
	
	/**
	 * 显示菜单栏
	 */
	private void showMenuBar() {
		JMenuBar menuBar = new JMenuBar();
		frmClumsycrawler.setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("Menu");
		menuBar.add(mnNewMenu);
		
		JMenu mnNewMenu_2 = new JMenu("Fetch Data");
		mnNewMenu.add(mnNewMenu_2);
		
		JMenuItem mntmFetchfromlocalfile = new JMenuItem("Fetch From Website");
		mntmFetchfromlocalfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//显示抓取器状态信息
				FetchDataMes.showMesDialog();
				//启动抓取器
				Action.Search();
			}
		});
		mnNewMenu_2.add(mntmFetchfromlocalfile);
		
		JMenu mnNewMenu_1 = new JMenu("Database Operation");
		mnNewMenu.add(mnNewMenu_1);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("Add Record");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				showPanelForAddRecord();
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem);
		
		JMenuItem mntmAddRecordsFrom = new JMenuItem("Add Records From LocalFile");
		mntmAddRecordsFrom.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 JFileChooser jf = new JFileChooser();  
		         jf.showOpenDialog(frmClumsycrawler);//显示打开的文件对话框  
		         
		         File f =  jf.getSelectedFile();//使用文件类获取选择器选择的文件  
		     
		         if(f!=null) {   
		        	 Action.pushIntoDBFromLocalFile(f.getAbsolutePath());
		         }else {
		        	 JOptionPane.showMessageDialog(frmClumsycrawler, "未指定文件");
		         }
			}
		});
		mnNewMenu_1.add(mntmAddRecordsFrom);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("Query");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showPanelForQuery();
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_1);
		
		frmClumsycrawler.getContentPane().repaint();// 重绘
	}
	
		
	/**
	 * 显示默认面板
	 */
	private void showDefaultPanel(){
		defaultPanel = new JPanel();
		defaultPanel.setBounds(0, 0, 923, 608);
		defaultPanel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("ClumsyCrawler");
		lblNewLabel.setForeground(Color.BLACK);
		lblNewLabel.setFont(new Font("SimSun", Font.BOLD, 35));
		lblNewLabel.setBounds(352, 170, 260, 41);
		defaultPanel.add(lblNewLabel);		

		JLabel lblAuthor = new JLabel("Author  huwei");
		lblAuthor.setFont(new Font("SimSun", Font.PLAIN, 22));
		lblAuthor.setBounds(416, 296, 143, 26);
		defaultPanel.add(lblAuthor);
		
		JLabel lblVersion = new JLabel("Version  2.0");
		lblVersion.setFont(new Font("SimSun", Font.PLAIN, 22));
		lblVersion.setBounds(426, 340, 132, 26);
		defaultPanel.add(lblVersion);
		
		frmClumsycrawler.getContentPane().add(defaultPanel);
		
		frmClumsycrawler.getContentPane().repaint();// 重绘
	}
	
	
	/*
	 * 显示添加记录的面板
	 */
	private void showPanelForAddRecord(){
		frmClumsycrawler.getContentPane().removeAll();// 清空
		
		panelForAddRecord = new JPanel();
		panelForAddRecord.setBounds(10, 10, 913, 598);
		frmClumsycrawler.getContentPane().add(panelForAddRecord);
		panelForAddRecord.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Country");
		lblNewLabel.setBounds(159, 48, 63, 18);
		lblNewLabel.setFont(new Font("SimSun", Font.BOLD, 15));
		panelForAddRecord.add(lblNewLabel);
		
		textField_1 = new JTextField();
		textField_1.setBounds(259, 47, 150, 21);
		textField_1.setToolTipText("");
		panelForAddRecord.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblUniversity = new JLabel("University");
		lblUniversity.setBounds(481, 48, 90, 18);
		lblUniversity.setFont(new Font("SimSun", Font.BOLD, 15));
		panelForAddRecord.add(lblUniversity);
		
		textField_2 = new JTextField();
		textField_2.setBounds(611, 47, 150, 21);
		panelForAddRecord.add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblSchool = new JLabel("School");
		lblSchool.setBounds(159, 97, 112, 18);
		lblSchool.setFont(new Font("SimSun", Font.BOLD, 15));
		panelForAddRecord.add(lblSchool);
		
		textField_5 = new JTextField();
		textField_5.setBounds(259, 143, 150, 21);
		panelForAddRecord.add(textField_5);
		textField_5.setColumns(10);
		
		JLabel lblProgramName = new JLabel("Program Name");
		lblProgramName.setBounds(481, 97, 119, 18);
		lblProgramName.setFont(new Font("SimSun", Font.BOLD, 15));
		panelForAddRecord.add(lblProgramName);
		
		textField_4 = new JTextField();
		textField_4.setBounds(611, 96, 150, 21);
		panelForAddRecord.add(textField_4);
		textField_4.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("HomePage");
		lblNewLabel_1.setBounds(159, 143, 90, 21);
		lblNewLabel_1.setFont(new Font("SimSun", Font.BOLD, 15));
		panelForAddRecord.add(lblNewLabel_1);
		
		textField_7 = new JTextField();
		textField_7.setBounds(259, 187, 150, 21);
		panelForAddRecord.add(textField_7);
		textField_7.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Location");
		lblNewLabel_2.setBounds(481, 144, 72, 18);
		lblNewLabel_2.setFont(new Font("SimSun", Font.BOLD, 15));
		panelForAddRecord.add(lblNewLabel_2);
		
		textField_6 = new JTextField();
		textField_6.setBounds(611, 143, 150, 21);
		panelForAddRecord.add(textField_6);
		textField_6.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("Email");
		lblNewLabel_3.setBounds(159, 188, 45, 18);
		lblNewLabel_3.setFont(new Font("SimSun", Font.BOLD, 15));
		panelForAddRecord.add(lblNewLabel_3);
		
		textField_3 = new JTextField();
		textField_3.setBounds(259, 96, 150, 21);
		panelForAddRecord.add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblPhoneNumber = new JLabel("Phone Number");
		lblPhoneNumber.setFont(new Font("SimSun", Font.BOLD, 15));
		lblPhoneNumber.setBounds(481, 190, 112, 15);
		panelForAddRecord.add(lblPhoneNumber);
		
		textField_8 = new JTextField();
		textField_8.setBounds(611, 187, 150, 21);
		panelForAddRecord.add(textField_8);
		textField_8.setColumns(10);
		
		JLabel lblDegree = new JLabel("Degree");
		lblDegree.setFont(new Font("SimSun", Font.BOLD, 15));
		lblDegree.setBounds(159, 233, 54, 15);
		panelForAddRecord.add(lblDegree);
		
		textField_9 = new JTextField();
		textField_9.setBounds(259, 230, 150, 21);
		panelForAddRecord.add(textField_9);
		textField_9.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("DDL With Aid");
		lblNewLabel_4.setFont(new Font("SimSun", Font.BOLD, 15));
		lblNewLabel_4.setBounds(481, 233, 119, 15);
		panelForAddRecord.add(lblNewLabel_4);
		
		textField_10 = new JTextField();
		textField_10.setBounds(611, 230, 150, 21);
		panelForAddRecord.add(textField_10);
		textField_10.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("DDL Without Aid");
		lblNewLabel_5.setFont(new Font("SimSun", Font.BOLD, 15));
		lblNewLabel_5.setBounds(480, 272, 150, 15);
		panelForAddRecord.add(lblNewLabel_5);
		
		textField_12 = new JTextField();
		textField_12.setBounds(621, 269, 140, 21);
		panelForAddRecord.add(textField_12);
		textField_12.setColumns(10);
		
		textField_11 = new JTextField();
		textField_11.setText("不填写则自动生成");
		textField_11.setBounds(259, 269, 150, 21);
		panelForAddRecord.add(textField_11);
		textField_11.setColumns(10);
		
		JLabel lblId = new JLabel("Id");
		lblId.setFont(new Font("SimSun", Font.BOLD, 15));
		lblId.setBounds(159, 272, 54, 15);
		panelForAddRecord.add(lblId);
		
		JButton btnNewButton = new JButton("Add It");
		btnNewButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				if(textField_1.getText().isEmpty()||textField_2.getText().isEmpty()||textField_3.getText().isEmpty()||textField_4.getText().isEmpty()||
						textField_5.getText().isEmpty()||textField_6.getText().isEmpty()||textField_7.getText().isEmpty()||textField_8.getText().isEmpty()||
						textField_9.getText().isEmpty()||textField_10.getText().isEmpty()||textField_11.getText().isEmpty()||textField_12.getText().isEmpty())
				{
					JOptionPane.showMessageDialog(frmClumsycrawler,"Incomplete Information");
				}else {
					Program p = new Program();
					p.setCountry(textField_1.getText());
					p.setUniversity(textField_2.getText());
					p.setSchool(textField_3.getText());
					p.setProgramName(textField_4.getText());
					p.setHomepage(textField_5.getText());
					p.setLocation(textField_6.getText());
					p.setEmail(textField_7.getText());
					p.setPhoneNumber(textField_8.getText());
					p.setDegree(textField_9.getText());
					p.setDeadlineWithAid(textField_10.getText());
					p.setDeadlineWithoutAid(textField_12.getText());
					if(!textField_11.getText().equals("不填写则自动生成")) {//如果用户自行填写
						p.setId(textField_11.getText());
					}else {										//使用默认值, 则自动生成
						p.setId(Action.getRandomNumber());
					}
					Action.addRecord(p);
					JOptionPane.showMessageDialog(frmClumsycrawler,"Add Successfully");
				}
			}
		});
		btnNewButton.setFont(new Font("SimSun", Font.BOLD, 20));
		btnNewButton.setBounds(302, 370, 368, 53);
		panelForAddRecord.add(btnNewButton);
		
		frmClumsycrawler.getContentPane().repaint();//重绘
	}
}
